 package com.gmrz.uaf.protocol.v1.validaton;
 
 import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.gmrz.uaf.common.Constants;
import com.gmrz.uaf.protocol.v1.schema.OperationHeader;
import com.gmrz.uaf.protocol.v1.schema.ServerChallenge;
import com.gmrz.uaf.server.ServerConfig;
 
 public class OperationHeaderValidator
   implements Validator<OperationHeader>
 {
   private static final Logger LOG = LogManager.getLogger(OperationHeaderValidator.class);
   private List<Validator<?>> validationChain;
   private ServerChallenge sc;
 
   public OperationHeaderValidator(ServerConfig config, Constants.Operation op,String appId,String transType)
   {
     this.validationChain = new ArrayList<Validator<?>>();
     this.validationChain.add(new OperationValidator(op));
     this.validationChain.add(new VersionValidator());
     this.validationChain.add(new AppIDValidator(config.getApplicationID(appId,transType),appId,transType));
     this.validationChain.add(new ServerDataValidator(config, op));
   }
 
   public void validate(OperationHeader header) throws HeaderValidationException
   {
     LOG.info("Validating {} OperationHeader", header.getOperation());
     for (Validator v : this.validationChain)
       if ((v instanceof VersionValidator)) {
         ((VersionValidator)v).validate(header.getProtocolVersion());
       } else if ((v instanceof OperationValidator)) {
         ((OperationValidator)v).validate(header.getOperation());
       } else if ((v instanceof AppIDValidator)) {
         ((AppIDValidator)v).validate(header.getApplicationID());
       } else if ((v instanceof ServerDataValidator)) {
         ((ServerDataValidator)v).setServerChallenge(this.sc);
         ((ServerDataValidator)v).validate(header.getServerData());
       }
   }
 
   public void setServerChallenge(ServerChallenge sc)
   {
     this.sc = sc;
   }
 }
